package org.example.car.system.controller;

import org.example.car.system.config.ExcelDownloader;
import org.example.car.system.config.R;
import org.example.car.system.domain.SysDictData;
import org.example.car.system.domain.SysDictType;
import org.example.car.system.service.SysDictDataService;
import org.example.car.system.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("type")
public class TypeController {

    @Autowired
    SysDictTypeService sysDictTypeService;

    @Autowired
    SysDictDataService sysDictDataService;

    @RequestMapping("list")
    public R list(@RequestBody SysDictType sysDictType){
        System.out.println(sysDictType);
        List<SysDictType> list = sysDictTypeService.getlist(sysDictType);
        return R.successed(list);
    }

    @RequestMapping("update")
    public R update(@RequestBody SysDictType sysDictType){
        sysDictType.setUpdateBy("admin");
        sysDictType.setUpdateTime(new Date());
        sysDictTypeService.updateById(sysDictType);
        return R.successed(null);
    }

    @RequestMapping("del")
    public R del(String dictId){
        List<String> list = Arrays.asList(dictId.split(","));
        sysDictTypeService.removeBatchByIds(list);
        return R.successed(null);
    }

    @RequestMapping("excel")
    public void excel(HttpServletResponse response) throws IOException {
        List<SysDictType> list = sysDictTypeService.list();
        ExcelDownloader.download(response,list,SysDictType.class,"字典");
    }

    @RequestMapping("add")
    public R add(@RequestBody SysDictType sysDictType){
        sysDictType.setCreateBy("admin");
        sysDictType.setCreateTime(new Date());
        sysDictTypeService.save(sysDictType);
        return R.successed(null);
    }

    @RequestMapping("dataList")
    public R dataList(@RequestBody SysDictData sysDictData){
       List<SysDictData> list = sysDictDataService.getlist(sysDictData);
       return R.successed(list);
    }

    @RequestMapping("typeList")
    public R typeList(){
        List<SysDictType> list = sysDictTypeService.list();
        return R.successed(list);
    }

    @RequestMapping("updateData")
    public R update(@RequestBody SysDictData sysDictData){
        sysDictData.setUpdateBy("admin");
        sysDictData.setUpdateTime(new Date());
        sysDictDataService.updateById(sysDictData);
        return R.successed(null);
    }

    @RequestMapping("addDate")
    public R addDate(@RequestBody SysDictData sysDictData){
        sysDictData.setCreateBy("admin");
        sysDictData.setCreateTime(new Date());
        sysDictDataService.save(sysDictData);
        return R.successed(null);
    }

    @RequestMapping("delData")
    public R del(Integer dictId){
        sysDictDataService.removeById(dictId);
        return R.successed(null);
    }
}
